package com.example.backend_422.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.backend_422.common.Result;
import com.example.backend_422.common.Utils;
import com.example.backend_422.entity.House;
import com.example.backend_422.entity.Rent;
import com.example.backend_422.entity.Reservation;
import com.example.backend_422.mapper.HouseMapper;
import com.example.backend_422.mapper.RentMapper;
import com.example.backend_422.mapper.ReservationMapper;
import com.example.backend_422.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements ReservationService {

    @Autowired
    private ReservationMapper reservationMapper; // 自动装配, 通过 @Autowired 自动装配 RentMapper

    @Autowired
    private RentMapper rentMapper; // 自动装配, 通过 @Autowired 自动装配 RentMapper

    @Autowired
    private Utils utils; // 自动装配, 通过 @Autowired 自动装配 Utils

    /**
     * 根据用户id获取预约信息
     * @param landlordId
     * @return
     */
    public Result getByLandlordId(Integer landlordId) {
        // 构造查询条件
        QueryWrapper<Reservation> wrapper = new QueryWrapper<>();
        wrapper.eq("landlord_id",landlordId);
        // 返回查询结果
        return Result.succ(reservationMapper.selectList(wrapper));
    }

    /**
     * 根据用户id获取预约信息
     * @param userId
     * @return
     */
    public Result getByUserId(Integer userId){
        // 构造查询条件
        QueryWrapper<Reservation> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);// 查询条件 user_id = userId
        // 返回查询结果
        return Result.succ(reservationMapper.selectList(wrapper));
    }
    @Autowired
    private HouseMapper houseMapper; // 自动装配, 通过 @Autowired 自动装配 HouseMapper

    /**
     * 根据预约id修改预约状态
     * @param id
     * @param status
     * @return
     */
    public Result confirmReservation(Integer id, String status,Integer houseId){
        if(status.equals("已拒绝")){
            // 拒绝预约
            UpdateWrapper<Reservation> wrapper = new UpdateWrapper<>();
            wrapper.eq("id",id);
            wrapper.set("status",status);
            return Result.succ(reservationMapper.update(null,wrapper));
        }
        if(houseId != null){
            // 同意预约, 判断房屋是否已经租出去了
            QueryWrapper<Rent> rentQueryWrapper = new QueryWrapper<>();
            // 查询条件 house_id = houseId, 并且按照 rent_id 降序排序, 取第一个
            rentQueryWrapper.eq("house_id",houseId).orderBy(false,true,"rent_id").last("limit 1");
            // 返回查询结果
            Rent rent = rentMapper.selectOne(rentQueryWrapper);
            if(rent != null){
                // 判断房屋是否已经租出去了, 如果已经租出去了, 则返回错误信息,通过比较两个日期的大小来判断,
                Boolean res = utils.CompareTwoDate(rent.getEndTime(),utils.getNowDate());
                if(res){
                    return Result.fail("房屋已经租出去了");
                }
            }
        }
        // 如果房屋没有租出去, 则同意预约,
        UpdateWrapper<Reservation> wrapper = new UpdateWrapper<>();// 构造更新条件
        wrapper.eq("id",id); // 更新条件 id = id
        wrapper.set("status",status); // 更新字段 status = status
        QueryWrapper<Reservation> queryWrapper = new QueryWrapper<>();// 构造查询条件
        queryWrapper.eq("id",id); // 查询条件 id = id
        Reservation reservation = reservationMapper.selectOne(queryWrapper); // 查询预约信息
        Rent rent = new Rent(); // 构造租赁信息
        // 设置租赁信息
        rent.setHouseId(houseId);
        rent.setHouseName(reservation.getHouseName());
        rent.setLandlordId(reservation.getLandlordId());
        rent.setUserId(reservation.getUserId());
        rent.setStartTime(reservation.getStartTime());
        rent.setEndTime(reservation.getEndTime());
        rent.setUserId(reservation.getUserId());
        rent.setUsername(reservation.getUsername());
        rent.setLandlordName(reservation.getLandlordName());
        // 插入租赁信息
        rentMapper.insert(rent);
        // 更新房屋状态
        UpdateWrapper<House> houseUpdateWrapper = new UpdateWrapper<>();
        houseUpdateWrapper.eq("house_id",houseId);
        houseUpdateWrapper.set("status","已租");
        // 返回更新结果
        houseMapper.update(null,houseUpdateWrapper);
        // 返回更新结果
        return Result.succ(reservationMapper.update(null,wrapper));
    }

    /**
     * 添加预约信息
     * @param userId
     * @param username
     * @param landlordId
     * @param landlordName
     * @param houseId
     * @param houseName
     * @param startTime
     * @param endTime
     * @return
     */
    public Result addReservation(Integer userId, String username, Integer landlordId, String landlordName,
                                 Integer houseId, String houseName, String startTime, String endTime) {
        // 判断房屋是否已经租出去了,和同意预约的逻辑一样
        QueryWrapper<Rent> rentQueryWrapper = new QueryWrapper<>();
        rentQueryWrapper.eq("house_id",houseId).orderBy(false,true,"rent_id").last("limit 1");
        Rent rent = rentMapper.selectOne(rentQueryWrapper);
        if(rent != null){
            Boolean res = utils.CompareTwoDate(rent.getEndTime(),utils.getNowDate());
            if(res){
                return Result.fail("房屋已经租出去了");
            }
        }
        // 如果房屋没有租出去, 则添加预约信息
        Reservation reservation = new Reservation();
        reservation.setUserId(userId);
        reservation.setUsername(username);
        reservation.setLandlordId(landlordId);
        reservation.setLandlordName(landlordName);
        reservation.setHouseId(houseId);
        reservation.setHouseName(houseName);
        reservation.setStartTime(utils.stringToDate(startTime));
        reservation.setEndTime(utils.stringToDate(endTime));
        // 返回添加结果
        reservationMapper.insert(reservation);
        // 返回添加结果
        return Result.succ("预约成功");


    }
}
